﻿using System;
using System.IO;
using System.Reflection;

class ItemResource
{
    public Int32 id { get; set; }
    public Int32 name_id { get; set; }
    public Int32 tooltip_id { get; set; }
    public Int32 type { get; set; }
    public Int32 group { get; set; }
    public Int32 Class { get; set; }
    public Int32 wear_type { get; set; }
    public Int32 Set_id { get; set; }
    public Int32 set_part_flag { get; set; }
    public Byte grade { get; set; }
    public Int32 rank { get; set; }
    public Int32 level { get; set; }
    public Int32 enhance { get; set; }
    public Int32 socket { get; set; }
    public Int32 status_flag { get; set; }
    public Int32 limit_deva { get; set; }
    public Int32 limit_asura { get; set; }
    public Int32 limit_gaia { get; set; }
    public Byte job_depth { get; set; }
    public Int32 limit_fighter { get; set; }
    public Int32 limit_hunter { get; set; }
    public Int32 limit_magician { get; set; }
    public Int32 limit_summoner { get; set; }
    public Int32 use_min_level { get; set; }
    public Int32 use_max_level { get; set; }
    public Int32 target_min_level { get; set; }
    public Int32 target_max_level { get; set; }
    public Single range { get; set; }
    public Single weight { get; set; }
    public Int32 price { get; set; }
    public Int32 huntaholic_point { get; set; }
    public Int32 ethereal_durability { get; set; }
    public Int32 endurance { get; set; }
    public Int32 material { get; set; }
    public Int32 summon_id { get; set; }
    public Int32 item_use_flag { get; set; }
    public Int32 available_period { get; set; }
    public Int16 decrease_type { get; set; }
    public Decimal throw_range { get; set; }
    public Int16 base_type_0 { get; set; }
    public Double base_var1_0 { get; set; }
    public Double base_var2_0 { get; set; }
    public Int16 base_type_1 { get; set; }
    public Double base_var1_1 { get; set; }
    public Double base_var2_1 { get; set; }
    public Int16 base_type_2 { get; set; }
    public Double base_var1_2 { get; set; }
    public Double base_var2_2 { get; set; }
    public Int16 base_type_3 { get; set; }
    public Double base_var1_3 { get; set; }
    public Double base_var2_3 { get; set; }
    public Int16 opt_type_0 { get; set; }
    public Double opt_var1_0 { get; set; }
    public Double opt_var2_0 { get; set; }
    public Int16 opt_type_1 { get; set; }
    public Double opt_var1_1 { get; set; }
    public Double opt_var2_1 { get; set; }
    public Int16 opt_type_2 { get; set; }
    public Double opt_var1_2 { get; set; }
    public Double opt_var2_2 { get; set; }
    public Int16 opt_type_3 { get; set; }
    public Double opt_var1_3 { get; set; }
    public Double opt_var2_3 { get; set; }
    public Int32 effect_id { get; set; }
    public Int16 enhance_0_id { get; set; }
    public Decimal enhance_0_01 { get; set; }
    public Decimal enhance_0_02 { get; set; }
    public Decimal enhance_0_03 { get; set; }
    public Decimal enhance_0_04 { get; set; }
    public Int16 enhance_1_id { get; set; }
    public Decimal enhance_1_01 { get; set; }
    public Decimal enhance_1_02 { get; set; }
    public Decimal enhance_1_03 { get; set; }
    public Decimal enhance_1_04 { get; set; }
    public Int32 skill_id { get; set; }
    public Int32 state_id { get; set; }
    public Int32 state_level { get; set; }
    public Int32 state_time { get; set; }
    public Int32 cool_time { get; set; }
    public Int16 cool_time_group { get; set; }
    public Int32 model_type_dem { get; set; }
    public Int32 model_type_def { get; set; }
    public Int32 model_type_asm { get; set; }
    public Int32 model_type_asf { get; set; }
    public Int32 model_type_gam { get; set; }
    public Int32 model_type_gaf { get; set; }
    public Byte deco_model_change { get; set; }
    public String model_00 { get; set; }
    public String model_01 { get; set; }
    public String model_02 { get; set; }
    public String model_03 { get; set; }
    public String model_04 { get; set; }
    public String model_05 { get; set; }
    public String model_06 { get; set; }
    public String model_07 { get; set; }
    public String model_08 { get; set; }
    public String model_09 { get; set; }
    public String model_10 { get; set; }
    public String model_11 { get; set; }
    public String model_12 { get; set; }
    public String model_13 { get; set; }
    public String model_14 { get; set; }
    public String model_15 { get; set; }
    public String model_16 { get; set; }
    public String model_17 { get; set; }
    public String texture_filename { get; set; }
    public String drop_type { get; set; }
    public Int32 icon_id { get; set; }
    public String icon_file_name { get; set; }
    public String script_text { get; set; }

    public void ReadFile(BinaryReader br)
    {
        this.id = br.ReadInt32();
        this.name_id = br.ReadInt32();
        this.type = br.ReadInt32();
        this.group = br.ReadInt32();
        this.Class = br.ReadInt32();
        this.Set_id = br.ReadInt32();
        this.set_part_flag = br.ReadInt32();
        this.grade = br.ReadByte();
        this.rank = br.ReadInt32();
        this.level = br.ReadInt32();
        this.enhance = br.ReadInt32();
        this.socket = br.ReadInt32();
        this.status_flag = br.ReadInt32();
        this.job_depth = br.ReadByte();
        this.use_min_level = br.ReadInt32();
        this.use_max_level = br.ReadInt32();
        this.target_min_level = br.ReadInt32();
        this.target_max_level = br.ReadInt32();
        this.range = Convert.ToSingle(br.ReadInt32()) / 100;
        this.weight = br.ReadSingle();
        this.price = br.ReadInt32();
        this.huntaholic_point = br.ReadInt32();
        this.ethereal_durability = br.ReadInt32();
        this.endurance = br.ReadInt32();
        this.wear_type = br.ReadInt32();
        Byte f = br.ReadByte();
        this.limit_deva = (f & 4) >> 2;
        this.limit_asura = (f & 8) >> 3;
        this.limit_gaia = (f & 16) >> 4;
        f = br.ReadByte();
        this.limit_fighter = (f & 4) >> 2;
        this.limit_hunter = (f & 8) >> 3;
        this.limit_magician = (f & 16) >> 4;
        this.limit_summoner = (f & 32) >> 5;
        br.ReadInt16();
        this.item_use_flag = br.ReadInt32();
        br.ReadUInt32(); // item_use_flag
        this.material = br.ReadInt32();
        this.summon_id = br.ReadInt32();
        this.throw_range = Convert.ToDecimal(br.ReadInt32());
        this.base_type_0 = br.ReadInt16();
        this.base_type_1 = br.ReadInt16();
        this.base_type_2 = br.ReadInt16();
        this.base_type_3 = br.ReadInt16();
        this.base_var1_0 = br.ReadDouble();
        this.base_var1_1 = br.ReadDouble();
        this.base_var1_2 = br.ReadDouble();
        this.base_var1_3 = br.ReadDouble();
        this.base_var2_0 = br.ReadDouble();
        this.base_var2_1 = br.ReadDouble();
        this.base_var2_2 = br.ReadDouble();
        this.base_var2_3 = br.ReadDouble();
        this.opt_type_0 = br.ReadInt16();
        this.opt_type_1 = br.ReadInt16();
        this.opt_type_2 = br.ReadInt16();
        this.opt_type_3 = br.ReadInt16();
        this.opt_var1_0 = br.ReadDouble();
        this.opt_var1_1 = br.ReadDouble();
        this.opt_var1_2 = br.ReadDouble();
        this.opt_var1_3 = br.ReadDouble();
        this.opt_var2_0 = br.ReadDouble();
        this.opt_var2_1 = br.ReadDouble();
        this.opt_var2_2 = br.ReadDouble();
        this.opt_var2_3 = br.ReadDouble();
        this.effect_id = br.ReadInt32();
        this.enhance_0_id = br.ReadInt16();
        this.enhance_1_id = br.ReadInt16();
        this.enhance_0_01 = Convert.ToDecimal(br.ReadSingle());
        this.enhance_0_02 = Convert.ToDecimal(br.ReadSingle());
        this.enhance_0_03 = Convert.ToDecimal(br.ReadSingle());
        this.enhance_0_04 = Convert.ToDecimal(br.ReadSingle());
        this.enhance_1_01 = Convert.ToDecimal(br.ReadSingle());
        this.enhance_1_02 = Convert.ToDecimal(br.ReadSingle());
        this.enhance_1_03 = Convert.ToDecimal(br.ReadSingle());
        this.enhance_1_04 = Convert.ToDecimal(br.ReadSingle());
        this.skill_id = br.ReadInt32();
        this.state_id = br.ReadInt32();
        this.state_level = br.ReadInt32();
        this.state_time = br.ReadInt32();
        this.cool_time = br.ReadInt32();
        this.cool_time_group = br.ReadInt16();
        this.available_period = br.ReadInt32();
        this.decrease_type = br.ReadInt16();
        br.ReadInt16(); // const 49
        this.model_type_dem = br.ReadInt32();
        this.model_type_def = br.ReadInt32();
        this.model_type_asm = br.ReadInt32();
        this.model_type_asf = br.ReadInt32();
        this.model_type_gam = br.ReadInt32();
        this.model_type_gaf = br.ReadInt32();
        this.deco_model_change = br.ReadByte();
        this.model_00 = SFM.BytesToString(br.ReadBytes(256));
        this.model_01 = SFM.BytesToString(br.ReadBytes(256));
        this.model_02 = SFM.BytesToString(br.ReadBytes(256));
        this.model_03 = SFM.BytesToString(br.ReadBytes(256));
        this.model_04 = SFM.BytesToString(br.ReadBytes(256));
        this.model_05 = SFM.BytesToString(br.ReadBytes(256));
        this.model_06 = SFM.BytesToString(br.ReadBytes(256));
        this.model_07 = SFM.BytesToString(br.ReadBytes(256));
        this.model_08 = SFM.BytesToString(br.ReadBytes(256));
        this.model_09 = SFM.BytesToString(br.ReadBytes(256));
        this.model_10 = SFM.BytesToString(br.ReadBytes(256));
        this.model_11 = SFM.BytesToString(br.ReadBytes(256));
        this.model_12 = SFM.BytesToString(br.ReadBytes(256));
        this.model_13 = SFM.BytesToString(br.ReadBytes(256));
        this.model_14 = SFM.BytesToString(br.ReadBytes(256));
        this.model_15 = SFM.BytesToString(br.ReadBytes(256));
        this.model_16 = SFM.BytesToString(br.ReadBytes(256));
        this.model_17 = SFM.BytesToString(br.ReadBytes(256));
        this.texture_filename = SFM.BytesToString(br.ReadBytes(256));
        this.drop_type = SFM.BytesToString(br.ReadBytes(256));
        BitConverter.ToString(br.ReadBytes(3)); // Cat
        this.icon_id = br.ReadInt32();
        this.icon_file_name = SFM.BytesToString(br.ReadBytes(256));
        this.script_text = SFM.BytesToString(br.ReadBytes(512));
        this.tooltip_id = br.ReadInt32();
    }

    public void SaveFile(BinaryWriter bw)
    {
        bw.Write(this.id);
        bw.Write(this.name_id);
        bw.Write(this.type);
        bw.Write(this.group);
        bw.Write(this.Class);
        bw.Write(this.Set_id);
        bw.Write(this.set_part_flag);
        bw.Write(this.grade);
        bw.Write(this.rank);
        bw.Write(this.level);
        bw.Write(this.enhance);
        bw.Write(this.socket);
        bw.Write(this.status_flag);
        bw.Write(this.job_depth);
        bw.Write(this.use_min_level);
        bw.Write(this.use_max_level);
        bw.Write(this.target_min_level);
        bw.Write(this.target_max_level);
        bw.Write(Convert.ToInt32(this.range * 100));
        bw.Write(this.weight);
        bw.Write(this.price);
        bw.Write(this.huntaholic_point);
        bw.Write(this.ethereal_durability);
        bw.Write(this.endurance);
        bw.Write(this.wear_type);
        Byte f = 0;
        if (this.limit_deva != 0) f |= 4;
        if (this.limit_asura != 0) f |= 8;
        if (this.limit_gaia != 0) f |= 16;
        bw.Write(f);
        Byte c = 0;
        if (this.limit_fighter != 0) c |= 4;
        if (this.limit_hunter != 0) c |= 8;
        if (this.limit_magician != 0) c |= 16;
        if (this.limit_summoner != 0) c |= 32;
        bw.Write(c);
        bw.Write(new Byte[2]);
        bw.Write(this.item_use_flag);
        bw.Write(this.item_use_flag);
        bw.Write(this.material);
        bw.Write(this.summon_id);
        bw.Write(Convert.ToInt32(this.throw_range));
        bw.Write(this.base_type_0);
        bw.Write(this.base_type_1);
        bw.Write(this.base_type_2);
        bw.Write(this.base_type_3);
        bw.Write(this.base_var1_0);
        bw.Write(this.base_var1_1);
        bw.Write(this.base_var1_2);
        bw.Write(this.base_var1_3);
        bw.Write(this.base_var2_0);
        bw.Write(this.base_var2_1);
        bw.Write(this.base_var2_2);
        bw.Write(this.base_var2_3);
        bw.Write(this.opt_type_0);
        bw.Write(this.opt_type_1);
        bw.Write(this.opt_type_2);
        bw.Write(this.opt_type_3);
        bw.Write(this.opt_var1_0);
        bw.Write(this.opt_var1_1);
        bw.Write(this.opt_var1_2);
        bw.Write(this.opt_var1_3);
        bw.Write(this.opt_var2_0);
        bw.Write(this.opt_var2_1);
        bw.Write(this.opt_var2_2);
        bw.Write(this.opt_var2_3);
        bw.Write(this.effect_id);
        bw.Write(this.enhance_0_id);
        bw.Write(this.enhance_1_id);
        bw.Write(Convert.ToSingle(this.enhance_0_01));
        bw.Write(Convert.ToSingle(this.enhance_0_02));
        bw.Write(Convert.ToSingle(this.enhance_0_03));
        bw.Write(Convert.ToSingle(this.enhance_0_04));
        bw.Write(Convert.ToSingle(this.enhance_1_01));
        bw.Write(Convert.ToSingle(this.enhance_1_02));
        bw.Write(Convert.ToSingle(this.enhance_1_03));
        bw.Write(Convert.ToSingle(this.enhance_1_04));
        bw.Write(this.skill_id);
        bw.Write(this.state_id);
        bw.Write(this.state_level);
        bw.Write(this.state_time);
        bw.Write(this.cool_time);
        bw.Write(this.cool_time_group);
        bw.Write(this.available_period);
        bw.Write(this.decrease_type);
        bw.Write(Convert.ToInt16(49));
        bw.Write(this.model_type_dem);
        bw.Write(this.model_type_def);
        bw.Write(this.model_type_asm);
        bw.Write(this.model_type_asf);
        bw.Write(this.model_type_gam);
        bw.Write(this.model_type_gaf);
        bw.Write(this.deco_model_change);
        bw.Write(SFM.StringToBytes(this.model_00));
        bw.Write(new Byte[256 - this.model_00.Length]);
        bw.Write(SFM.StringToBytes(this.model_01));
        bw.Write(new Byte[256 - this.model_01.Length]);
        bw.Write(SFM.StringToBytes(this.model_02));
        bw.Write(new Byte[256 - this.model_02.Length]);
        bw.Write(SFM.StringToBytes(this.model_03));
        bw.Write(new Byte[256 - this.model_03.Length]);
        bw.Write(SFM.StringToBytes(this.model_04));
        bw.Write(new Byte[256 - this.model_04.Length]);
        bw.Write(SFM.StringToBytes(this.model_05));
        bw.Write(new Byte[256 - this.model_05.Length]);
        bw.Write(SFM.StringToBytes(this.model_06));
        bw.Write(new Byte[256 - this.model_06.Length]);
        bw.Write(SFM.StringToBytes(this.model_07));
        bw.Write(new Byte[256 - this.model_07.Length]);
        bw.Write(SFM.StringToBytes(this.model_08));
        bw.Write(new Byte[256 - this.model_08.Length]);
        bw.Write(SFM.StringToBytes(this.model_09));
        bw.Write(new Byte[256 - this.model_09.Length]);
        bw.Write(SFM.StringToBytes(this.model_10));
        bw.Write(new Byte[256 - this.model_10.Length]);
        bw.Write(SFM.StringToBytes(this.model_11));
        bw.Write(new Byte[256 - this.model_11.Length]);
        bw.Write(SFM.StringToBytes(this.model_12));
        bw.Write(new Byte[256 - this.model_12.Length]);
        bw.Write(SFM.StringToBytes(this.model_13));
        bw.Write(new Byte[256 - this.model_13.Length]);
        bw.Write(SFM.StringToBytes(this.model_14));
        bw.Write(new Byte[256 - this.model_14.Length]);
        bw.Write(SFM.StringToBytes(this.model_15));
        bw.Write(new Byte[256 - this.model_15.Length]);
        bw.Write(SFM.StringToBytes(this.model_16));
        bw.Write(new Byte[256 - this.model_16.Length]);
        bw.Write(SFM.StringToBytes(this.model_17));
        bw.Write(new Byte[256 - this.model_17.Length]);
        bw.Write(SFM.StringToBytes(this.texture_filename));
        bw.Write(new Byte[256 - this.texture_filename.Length]);
        bw.Write(SFM.StringToBytes(this.drop_type));
        bw.Write(new Byte[256 - this.drop_type.Length]);
        bw.Write(SFM.StringToBytes("Cat"));
        bw.Write(this.icon_id);
        bw.Write(SFM.StringToBytes(this.icon_file_name));
        bw.Write(new Byte[256 - this.icon_file_name.Length]);
        bw.Write(SFM.StringToBytes(this.script_text));
        bw.Write(new Byte[512 - this.script_text.Length]);
        bw.Write(this.tooltip_id);
    }

    public string ToDatabase()
    {
        return "INSERT [dbo].[ItemResource] ([id], [name_id], [tooltip_id], [type], [group], [class], [wear_type], [set_id], [set_part_flag], [grade], [rank], [level], [enhance], [socket], [status_flag], [limit_deva], [limit_asura], [limit_gaia], [job_depth], [limit_fighter], [limit_hunter], [limit_magician], [limit_summoner], [use_min_level], [use_max_level], [target_min_level], [target_max_level], [range], [weight], [price], [huntaholic_point], [ethereal_durability], [endurance], [material], [summon_id], [item_use_flag], [available_period], [decrease_type], [throw_range], [base_type_0], [base_var1_0], [base_var2_0], [base_type_1], [base_var1_1], [base_var2_1], [base_type_2], [base_var1_2], [base_var2_2], [base_type_3], [base_var1_3], [base_var2_3], [opt_type_0], [opt_var1_0], [opt_var2_0], [opt_type_1], [opt_var1_1], [opt_var2_1], [opt_type_2], [opt_var1_2], [opt_var2_2], [opt_type_3], [opt_var1_3], [opt_var2_3], [effect_id], [enhance_0_id], [enhance_0_01], [enhance_0_02], [enhance_0_03], [enhance_0_04], [enhance_1_id], [enhance_1_01], [enhance_1_02], [enhance_1_03], [enhance_1_04], [skill_id], [state_id], [state_level], [state_time], [cool_time], [cool_time_group], [model_type_dem], [model_type_def], [model_type_asm], [model_type_asf], [model_type_gam], [model_type_gaf], [deco_model_change], [model_00], [model_01], [model_02], [model_03], [model_04], [model_05], [model_06], [model_07], [model_08], [model_09], [model_10], [model_11], [model_12], [model_13], [model_14], [model_15], [model_16], [model_17], [texture_filename], [drop_type], [icon_id], [icon_file_name], [script_text]) VALUES ( @Pid, @Pname_id, @Ptooltip_id, @Ptype, @Pgroup, @Pclass, @Pwear_type, @Pset_id, @Pset_part_flag, @Pgrade, @Prank, @Plevel, @Penhance, @Psocket, @Pstatus_flag, @Plimit_deva, @Plimit_asura, @Plimit_gaia, @Pjob_depth, @Plimit_fighter, @Plimit_hunter, @Plimit_magician, @Plimit_summoner, @Puse_min_level, @Puse_max_level, @Ptarget_min_level, @Ptarget_max_level, @Prange, @Pweight, @Pprice, @Phuntaholic_point, @Pethereal_durability, @Pendurance, @Pmaterial, @Psummon_id, @Pitem_use_flag, @Pavailable_period, @Pdecrease_type, @Pthrow_range, @Pbase_type_0, @Pbase_var1_0, @Pbase_var2_0, @Pbase_type_1, @Pbase_var1_1, @Pbase_var2_1, @Pbase_type_2, @Pbase_var1_2, @Pbase_var2_2, @Pbase_type_3, @Pbase_var1_3, @Pbase_var2_3, @Popt_type_0, @Popt_var1_0, @Popt_var2_0, @Popt_type_1, @Popt_var1_1, @Popt_var2_1, @Popt_type_2, @Popt_var1_2, @Popt_var2_2, @Popt_type_3, @Popt_var1_3, @Popt_var2_3, @Peffect_id, @Penhance_0_id, @Penhance_0_01, @Penhance_0_02, @Penhance_0_03, @Penhance_0_04, @Penhance_1_id, @Penhance_1_01, @Penhance_1_02, @Penhance_1_03, @Penhance_1_04, @Pskill_id, @Pstate_id, @Pstate_level, @Pstate_time, @Pcool_time, @Pcool_time_group, @Pmodel_type_dem, @Pmodel_type_def, @Pmodel_type_asm, @Pmodel_type_asf, @Pmodel_type_gam, @Pmodel_type_gaf, @Pdeco_model_change, @Pmodel_00, @Pmodel_01, @Pmodel_02, @Pmodel_03, @Pmodel_04, @Pmodel_05, @Pmodel_06, @Pmodel_07, @Pmodel_08, @Pmodel_09, @Pmodel_10, @Pmodel_11, @Pmodel_12, @Pmodel_13, @Pmodel_14, @Pmodel_15, @Pmodel_16, @Pmodel_17, @Ptexture_filename, @Pdrop_type, @Picon_id, @Picon_file_name, @Pscript_text )";
    }

    public static String FromDatabase()
    {
        String rString = "SELECT ";
        foreach (FieldInfo fieldInfo in typeof(ItemResource).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static))
        {
            rString += string.Format("[{0}], ", fieldInfo.Name.Substring(1, fieldInfo.Name.IndexOf('>') - 1));
        }
        return rString.Substring(0, rString.Length - 2) + " FROM ItemResource ORDER BY id";
    }

    public void FromDatabaseExecute(System.Data.SqlClient.SqlDataReader drExecute)
    {
        foreach (FieldInfo fieldInfo in typeof(ItemResource).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static))
        {
            String sTempName = null;
            sTempName = fieldInfo.Name.Substring(1, fieldInfo.Name.IndexOf('>') - 1);

            if (fieldInfo.FieldType == typeof(String))
            {
                fieldInfo.SetValue(this, drExecute[sTempName].ToString());
            }
            else if (fieldInfo.FieldType == typeof(Int16))
            {
                fieldInfo.SetValue(this, Convert.ToInt16(drExecute[sTempName]));
            }
            else if (fieldInfo.FieldType == typeof(Int32))
            {
                fieldInfo.SetValue(this, Convert.ToInt32(drExecute[sTempName]));
            }
            else if (fieldInfo.FieldType == typeof(UInt32))
            {
                fieldInfo.SetValue(this, Convert.ToUInt32(drExecute[sTempName]));
            }
            else if (fieldInfo.FieldType == typeof(Single))
            {
                fieldInfo.SetValue(this, Convert.ToSingle(drExecute[sTempName]));
            }
            else if (fieldInfo.FieldType == typeof(Decimal))
            {
                fieldInfo.SetValue(this, Convert.ToDecimal(drExecute[sTempName]));
            }
            else if (fieldInfo.FieldType == typeof(Double))
            {
                fieldInfo.SetValue(this, Convert.ToDouble(drExecute[sTempName]));
            }
            else if (fieldInfo.FieldType == typeof(Byte))
            {
                fieldInfo.SetValue(this, Convert.ToByte(drExecute[sTempName]));
            }
        }
    }
}